package com.sx.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sx.bean.Role;
import com.sx.dao.PtRRoleOrganDao;
import com.sx.dao.RoleDao;
import com.sx.service.RoleService;
import com.sx.util.Pagination;
import com.sx.bean.Node;
import com.sx.bean.PtRRoleOrgan;
import com.sx.bean.PtUser;

@Service("roleService")
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleDao roleDao;
	@Autowired
	private PtRRoleOrganDao ptRRoleOrganDao;
	
	@Override
	public void insert(PtRRoleOrgan ptrroleorgan) {
		ptRRoleOrganDao.insert(ptrroleorgan);
	}

	@Override
	public List<Node> getByParentId(String parentId) {
		List<Node> list=roleDao.getByParentId(parentId);
		for (Node node : list) {
			List<Node> listchildren=roleDao.getByParentId(node.getId());
			if (listchildren.isEmpty()) {
				node.setHasChildren(false);
			}else {
				
				  for (Node node2 : listchildren) { node2.setHasChildren(false); }
				 
				node.setChildren(listchildren);
			}
		}
		return list;
	}

	@Override
	public List<Role> selectAll() {
		return roleDao.selectAll();
	}

	@Override
	public Role selectRoleByUUid(String role_uuid) {
		return roleDao.selectRoleByUUid(role_uuid);
	}

	@Override
	public void updateRoleByUUid(String role_uuid, String role_id, String role_name) {
		roleDao.updateRoleByUUid(role_uuid, role_id, role_name);
	}

	@Override
	public void insertRole(String role_uuid, String role_id, String role_name) {
		roleDao.insertRole(role_uuid, role_id, role_name);
	}

	@Override
	public void deleteRoleByUUid(String role_uuid) {
		roleDao.deleteRoleByUUid(role_uuid);
	}

	@Override
	public int insertRoleRes(String role_uuid, String[] res_uuid) {
		String[] arrayStr =roleDao.selectResByUUid(role_uuid);
		int data =1;
		for (String s1:arrayStr) {
			for (String s2 : res_uuid) {
				System.out.println(s1.equals(s2));
				if (s1.equals(s2)) {
					System.out.println(s2);
					data=0;
					break;
				}
			}
			if (data == 0) {
				break;
			}
		}
		if (data == 1) {
			for (String string :res_uuid) {
				roleDao.insertRoleRes(role_uuid, string);
			}
		}
		return data;
	}

	@Override
	public Pagination<Role> getRoles(int pageNumber, int pageSize) {
		int count = roleDao.getCount();
		List<Role> list = roleDao.getRoles((pageNumber-1)*pageSize,pageSize);
		Pagination<Role> list2 = new Pagination<Role>();
		list2.setPageSize(pageSize);
		list2.setPageNumber(pageNumber);
		list2.setTotal(count);
		list2.setRows(list);
		return list2;
	}



	
	
}
